<template>
	<datetime :placeholder="placeholder ? placeholder : '请选择'" v-model="val" v-bind:start-date="startDate" :readonly="abPermission==='r' || disabled" :format="format|| currentFormat" v-bind:end-date="endDate"></datetime>
</template>

<script>
import Vue from 'vue'
import { Datetime } from 'vux'
Vue.component('datetime', Datetime)

export default {
  	props: ['value',"min","max","minformat","maxformat","cal","caltype","abPermission","format","abFormat","placeholder","disabled"],
  	data :function () {
	    return {
	    	val:this.value,
	    	startDate:"1900-01-01",
	    	endDate:"2099-12-31",
	    	currentFormat:'YYYY-MM-DD HH:mm'
	    }
	},
	created: function(){

		//yyyy-MM-dd HH:mm:ss
		// 兼容 vux 的特殊格式化
		if(this.abFormat){

			var format = this.abFormat.toUpperCase();
			format = format.replaceAll("SS","")
			format = format.replaceAll(":MM",":mm")
			this.currentFormat = format;
		}
	},
	mounted : function(){
		this.handleMin();
		this.handleMax();
 	},
  	methods: {
    	//计算差距 拿最小值跟自身比较
    	handleCal(){
    		if(!this.min||!this.val){
    			this.$emit('update:cal',"");
    			return;
    		}
        var minformat = this.minformat ||  'yyyy-MM-dd HH:mm';
        var maxformat = this.maxformat ||  'yyyy-MM-dd HH:mm';
        if(minformat === 'yyyy-MM-dd HH:mm:ss'){
          minformat = 'yyyy-MM-dd HH:mm';
        }
        if(maxformat === 'yyyy-MM-dd HH:mm:ss'){
          maxformat =  'yyyy-MM-dd HH:mm';
        }
    		var rtl = this.min.toDate(minformat).between(this.val.toDate(maxformat),this.caltype);
    		this.$emit('update:cal',rtl);
    	},
    	handleMin(){
    		this.handleCal();
 			if(!this.min){
 				// this.startDate = "";
 				return;
 			}
 			var date = this.min.toDate(this.minformat);
 			this.startDate = date.format("yyyy-MM-dd");
    	},
    	handleMax(){
        	this.endDate = "2099-12-31";
    // 		if(!this.max){
 			// 	this.endDate = "2099-12-31";
 			// 	return;
 			// }
 			// var date = this.max.toDate(this.maxformat);
 			// this.endDate = date.format("yyyy-MM-dd");
    	}
  	},
 	watch : {
 		val : function(newVal,oldVal){
 			this.$emit('input', this.val);
 			this.handleCal();
 		},
 		value : function(newVal,oldVal){
 			this.val = newVal;
 		},
 		min : function(newVal,oldVal){
 			this.handleMin();
 		},
 		max : function(newVal,oldVal){
 			this.handleMax();
 		}
 	}

}
</script>
